using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinTabControl;
using PluginFrame;

namespace PcrDataAnalysis
{
	public class FormRule : Form
	{
		private IApplication mApplication;

		private List<IPcrExperimentProject> mProjects;

		protected ResourceManager mResourceManager;

		protected IConfigurationService mConfigurationService;

		protected IPcrSystemService mPcrSystemService;

		protected IPcrLogService mPcrLogService;

		protected IAnalysisService mAnalysisService;

		protected IPcrExperimentProjectService mPcrExperimentProjectService;

		protected UltraTabControl mOptionsTabControl;

		protected IRuleEditorService mRuleEditorService;

		private IContainer components;

		protected Panel panel1;

		protected Button btnConfirm;

		protected Button btnCancel;

		public FormRule(IApplication app, List<IPcrExperimentProject> projects)
		{
			InitializeComponent();
			mApplication = app;
			mProjects = projects;
			mConfigurationService = (IConfigurationService)app.GetService("ConfigurationService");
			mPcrSystemService = (IPcrSystemService)app.GetService("PcrSystemService");
			mPcrLogService = (IPcrLogService)app.GetService("PcrLogService");
			mAnalysisService = (IAnalysisService)app.GetService("AnalysisService");
			mPcrExperimentProjectService = (IPcrExperimentProjectService)app.GetService("PcrExperimentProjectService");
			mResourceManager = ResourceManager.CreateFileBasedResourceManager("PcrDataAnalysis", app.GetResourceFolder() + GetType().Namespace + "\\", null);
			mRuleEditorService = (IRuleEditorService)app.GetService("RuleEditorService");
			base.Width = mConfigurationService.RuleOptionFormWidth;
			base.Height = mConfigurationService.RuleOptionFormHeight;
			InitializeRuleTab();
		}

		private void InitializeRuleTab()
		{
			IPcrExperiment activeFormCurrentExperiment = mPcrSystemService.GetActiveFormCurrentExperiment(null);
			bool isLockGrid = false;
			if (activeFormCurrentExperiment.HasPassword && !activeFormCurrentExperiment.PcrStructure.PcrDataFile.IsNewPcrFile && !activeFormCurrentExperiment.IsPasswordMatch)
			{
				isLockGrid = true;
			}
			mOptionsTabControl = mAnalysisService.GenerateRuleOptionTabControl(mProjects, isLockGrid);
			base.Controls.Add(mOptionsTabControl);
			mOptionsTabControl.BringToFront();
			panel1.SendToBack();
		}

		private void FormRule_Load(object sender, EventArgs e)
		{
			foreach (UltraTab tab in mOptionsTabControl.Tabs)
			{
				UltraTabControl ultraTabControl = tab.TabPage.Controls[0] as UltraTabControl;
				if (ultraTabControl == null)
				{
					continue;
				}
				UltraGrid ultraGrid = ultraTabControl.Tabs["Rule"].TabPage.Controls[0] as UltraGrid;
				if (ultraGrid == null)
				{
					continue;
				}
				IPcrExperimentProject pcrExperimentProject = ultraTabControl.Tag as IPcrExperimentProject;
				if (pcrExperimentProject != null)
				{
					mPcrExperimentProjectService.RefreshRuleGrid(ultraGrid, pcrExperimentProject);
					Panel panel = ultraTabControl.Tabs["AdvancedRule"].TabPage.Controls["advancedRulePanel"] as Panel;
					UltraGrid multraGridRule = panel.Controls[0].Controls[0].Controls[0] as UltraGrid;
					mRuleEditorService.RefreshRuleGrid(multraGridRule, pcrExperimentProject);
					if (pcrExperimentProject.IsUseAdvancedRule)
					{
						ultraTabControl.SelectedTab = ultraTabControl.Tabs["AdvancedRule"];
					}
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			Close();
		}

		private void btnConfirm_Click(object sender, EventArgs e)
		{
			try
			{
				foreach (UltraTab tab in mOptionsTabControl.Tabs)
				{
					if (!tab.Visible || !tab.Enabled)
					{
						continue;
					}
					UltraTabPageControl tabPage = tab.TabPage;
					UltraTabControl ultraTabControl = tabPage.Controls[0] as UltraTabControl;
					CheckBox checkBox = ultraTabControl.Tabs["Rule"].TabPage.Controls[1].Controls[0] as CheckBox;
					if (ultraTabControl == null)
					{
						continue;
					}
					IPcrExperimentProject pcrExperimentProject = ultraTabControl.Tag as IPcrExperimentProject;
					if (pcrExperimentProject == null)
					{
						continue;
					}
					if (checkBox.Checked)
					{
						UltraGrid ultraGrid = ultraTabControl.Tabs["Rule"].TabPage.Controls[0] as UltraGrid;
						if (ultraGrid != null && !mPcrExperimentProjectService.SaveRuleGrid(ultraGrid, pcrExperimentProject))
						{
							return;
						}
						continue;
					}
					Panel panel = ultraTabControl.Tabs["AdvancedRule"].TabPage.Controls["advancedRulePanel"] as Panel;
					if (panel == null)
					{
						return;
					}
					UltraGrid ultraGrid2 = panel.Controls[0].Controls[0].Controls[0] as UltraGrid;
					if (ultraGrid2 == null || !mRuleEditorService.SaveRuleGrid(ultraGrid2, pcrExperimentProject))
					{
						return;
					}
				}
				base.DialogResult = DialogResult.OK;
				Close();
			}
			catch (Exception ex)
			{
				mPcrLogService.ShowExceptionDialogBox(ex);
			}
		}

		private void FormRule_FormClosed(object sender, FormClosedEventArgs e)
		{
			mConfigurationService.RuleOptionFormWidth = base.Width;
			mConfigurationService.RuleOptionFormHeight = base.Height;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PcrDataAnalysis.FormRule));
			this.panel1 = new System.Windows.Forms.Panel();
			this.btnConfirm = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			base.SuspendLayout();
			this.panel1.AccessibleDescription = null;
			this.panel1.AccessibleName = null;
			resources.ApplyResources(this.panel1, "panel1");
			this.panel1.BackgroundImage = null;
			this.panel1.Controls.Add(this.btnConfirm);
			this.panel1.Controls.Add(this.btnCancel);
			this.panel1.Font = null;
			this.panel1.Name = "panel1";
			this.btnConfirm.AccessibleDescription = null;
			this.btnConfirm.AccessibleName = null;
			resources.ApplyResources(this.btnConfirm, "btnConfirm");
			this.btnConfirm.BackgroundImage = null;
			this.btnConfirm.Font = null;
			this.btnConfirm.Name = "btnConfirm";
			this.btnConfirm.UseVisualStyleBackColor = true;
			this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
			this.btnCancel.AccessibleDescription = null;
			this.btnCancel.AccessibleName = null;
			resources.ApplyResources(this.btnCancel, "btnCancel");
			this.btnCancel.BackgroundImage = null;
			this.btnCancel.Font = null;
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
			base.AccessibleDescription = null;
			base.AccessibleName = null;
			resources.ApplyResources(this, "$this");
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.BackgroundImage = null;
			base.Controls.Add(this.panel1);
			this.Font = null;
			base.Icon = null;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "FormRule";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			base.Load += new System.EventHandler(FormRule_Load);
			base.FormClosed += new System.Windows.Forms.FormClosedEventHandler(FormRule_FormClosed);
			this.panel1.ResumeLayout(false);
			base.ResumeLayout(false);
		}
	}
}
